package com.unieagle.exam.model;

import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class Answer implements Parcelable{
	
	private long id;
	
	private long questionId;
	
	private String content;
	
	private int correct;
	
	public Answer() {
	}
	
	public static Answer parse(JSONObject obj) {
		Answer ans = new Answer();
		ans.setId(obj.optLong("id"));
		ans.setContent(obj.optString("text"));
		if(obj.optBoolean("correct")) {
			ans.setCorrect(1);
		} else {
			ans.setCorrect(0);
		}
		return ans;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isCorrect() {
		return correct == 1;
	}
	
	public int getCorrect() {
		return correct;
	}

	public void setCorrect(int correct) {
		this.correct = correct;
	}

	@Override
	public boolean equals(Object o) {
		return this.id == ((Answer)o).getId();
	}

	public long getQuestionId() {
		return questionId;
	}

	public void setQuestionId(long questionId) {
		this.questionId = questionId;
	}
	
	public static final Parcelable.Creator<Answer> CREATOR = new Creator<Answer>() {
		
		@Override  
		public Answer createFromParcel(Parcel source) {  
			Answer ans = new Answer();
			ans.setId(source.readLong());
			ans.setQuestionId(source.readLong());
			ans.setContent(source.readString());
			ans.setCorrect(source.readInt());
			return ans;  
		}

		@Override
		public Answer[] newArray(int arg0) {
			return new Answer[arg0];
		}
	};

	@Override
	public int describeContents() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void writeToParcel(Parcel arg0, int arg1) {
		arg0.writeLong(id);
		arg0.writeLong(questionId);
		arg0.writeString(content);
		arg0.writeInt(correct);
	}
	
}
